<?php
class mdl_app extends CI_Model{ 
	
	var $tabel = 'macs_app';
	
	function __construct(){
		parent::__construct();
	} 

	function data($p=array()){
	
		$where = '';
		
		if( isset($p['column']) && isset($p['keyword']) ){
			if($p['column'] != '' || $p['keyword'] != ''){
				if($p['column'] == 'all'){
					$where = " AND ( app_name like '%".pTxt($p['keyword'])."%'
									 OR app_desc like '%".pTxt($p['keyword'])."%' 
									 OR app_domain like '%".pTxt($p['keyword'])."%' 
									 OR app_version like '%".pTxt($p['keyword'])."%' 
									 OR app_owner like '%".pTxt($p['keyword'])."%')";
				}else{				
					$where = " AND ".$p['column']." like '%".pTxt($p['keyword'])."%' ";
				}
			}
		}
		$sql = "
		 	select 
				SQL_CALC_FOUND_ROWS *
			FROM ".$this->tabel." 
			WHERE 1=1 
				".$where."
			ORDER BY app_id ASC 
		";	 
		
		if( isset($p['limit']) && isset($p['offset']) ){
			$offset = empty($p['offset'])?0:$p['offset'];				
			$sql .= " LIMIT ".$offset.",".$p['limit']." ";
		}
		$query = $this->db->query($sql);
		
		$found_rows = $this->db->query('SELECT FOUND_ROWS() as found_rows');

		return array(
			"data" 	=> $query->result(),
			"count"	=> $found_rows->row()
		);
	}	
	
}